
using Abp.Domain.Repositories;
using Abp.EntityFrameworkCore.Repositories;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace SOEI.Solution.DomainEntities.Parameters.DomainService
{
	/// <summary>
	/// 领域服务层一个模块的核心业务逻辑
	///</summary>
	public class ParameterManager : DomainServiceBase, IParameterManager
	{

		private readonly IRepository<Parameter, string> _parameterRepository;

		/// <summary>
		/// 【Parameter】的构造方法
		/// 通过构造函数注册服务到依赖注入容器中
		///</summary>
		public ParameterManager(IRepository<Parameter, string> parameterRepository)
		{
			_parameterRepository = parameterRepository;
		}

		#region -------------------------------------------------辅助工具生成---------------------------------------------- 

		/// <summary>
		/// 返回列表查询用
		/// </summary>
		/// <returns></returns>
		public IQueryable<Parameter> QueryEntityListAsNoTracking()
		{

			var query = _parameterRepository.GetAll().AsNoTracking()
						.Select(x => new Parameter
						{

							Id = x.Id,
							Value = x.Value,

						});
			return query;
		}

		/// <summary>
		/// 【Parameter】返回表达式数的实体信息即IQueryable类型
		/// </summary>
		/// <returns>IQueryable</returns>
		public IQueryable<Parameter> Query()
		{
			return _parameterRepository.GetAll();
		}
		/// <summary>
		/// 【Parameter】返回即IQueryable类型的实体，不包含EF Core跟踪标记
		/// </summary>
		/// <returns>IQueryable</returns>
		public IQueryable<Parameter> QueryAsNoTracking()
		{
			return _parameterRepository.GetAll().AsNoTracking();
		}
		/// <summary>
		/// 【Parameter】根据Id查询实体信息
		/// </summary>
		/// <param name="id"></param>
		/// <returns>实体</returns>
		public async Task<Parameter> FindByIdAsync(string id)
		{
			var entity = await _parameterRepository.GetAsync(id);
			return entity;
		}
		/// <summary>
		/// 【Parameter】检查实体是否存在
		/// </summary>
		/// <param name="id"></param>
		/// <returns>bool</returns>
		public async Task<bool> IsExistAsync(string id)
		{
			var result = await _parameterRepository.GetAll().AnyAsync(a => a.Id == id);
			return result;
		}
		/// <summary>
		/// 【Parameter】创建实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<Parameter> CreateAsync(Parameter entity)
		{
			entity.Id = await _parameterRepository.InsertAndGetIdAsync(entity);
			return entity;
		}
		/// <summary>
		/// 【Parameter】更新实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task UpdateAsync(Parameter entity)
		{
			await _parameterRepository.UpdateAsync(entity);
		}
		/// <summary>
		/// 【Parameter】删除实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task DeleteAsync(string id)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _parameterRepository.DeleteAsync(id);
		}
		/// <summary>
		/// 【Parameter】批量删除
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task BatchDelete(List<string> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _parameterRepository.DeleteAsync(a => input.Contains(a.Id));
		}
		#endregion

		#region -------------------------------------------------用户自定义------------------------------------------------
		/*请在此扩展领域服务接口*/

		/// <summary>
		/// 返回人员类型参数
		/// </summary>
		/// <returns></returns>
		public IQueryable<Parameter> QueryPersonType()
		{
			var sql = $"select RYLXDM AS ID,MC AS VALUE from CLFWQ_CS_RYLX"; // 人员类型查询的Sql语句
			var query = this._parameterRepository.GetDbContext().Set<Parameter>().FromSqlRaw(sql);
			return query;
		}

		/// <summary>
		/// 返回证件类型参数
		/// </summary>
		/// <returns></returns>
		public IQueryable<Parameter> QueryCertificateType()
		{
			var sql = "select ZJLXDM AS ID,MC AS VALUE from YW_CS_ZJLX where ZJLXDM in ('10','60','70','30','40','85','100110');"; // 人员类型查询的Sql语句
			var query = this._parameterRepository.GetDbContext().Set<Parameter>().FromSqlRaw(sql);
			return query;
		}

		/// <summary>获取监管协议状态参数
		/// </summary>
		/// <returns></returns>
		public IQueryable<Parameter> QueryAgreementState()
		{
			var sql = "SELECT A.JGXYZTDM AS ID, A.MC AS VALUE FROM CLFZJJG_CS_JGXYZT A WHERE A.JGXYZTDM <> '03'";
			var query = this._parameterRepository.GetDbContext().Set<Parameter>().FromSqlRaw(sql);
			return query;
		}

		/// <summary>
		/// 返回买受人购房情况类型参数
		/// </summary>
		/// <returns></returns>
		public IQueryable<Parameter> QueryBuyerConditionType()
		{
			var sql = "select BUYERCONDITION AS ID,MC AS VALUE from NESF_CS_BUYQUALIFICATION"; // 买受人购房情况类型查询的Sql语句
			var query = this._parameterRepository.GetDbContext().Set<Parameter>().FromSqlRaw(sql);
			return query;
		}

		/// <summary>
		/// 返回户籍所在类型参数
		/// </summary>
		/// <returns></returns>
		public IQueryable<Parameter> QueryCensusRegisterType()
		{
			var sql = "select ISBSDM AS ID,MC AS VALUE from NESF_CS_ISBS"; // 户籍所在类型查询的Sql语句
			var query = this._parameterRepository.GetDbContext().Set<Parameter>().FromSqlRaw(sql);
			return query;
		}

		/// <summary>
		/// 返回产权类型参数
		/// </summary>
		/// <returns></returns>
		public IQueryable<Parameter> QueryPropertyType()
		{
			var sql = "select CQLXDM AS ID,MC AS VALUE from NESF_CS_CQLX;"; // 人员类型查询的Sql语句
			var query = this._parameterRepository.GetDbContext().Set<Parameter>().FromSqlRaw(sql);
			return query;
		}
		/// <summary>
		/// 获取审核状态
		/// </summary>
		/// <returns></returns>
		public IQueryable<Parameter> QueryApprovalStatus()
		{
			var sql = "select ZTDM AS ID,ZTMC AS VALUE from CLFWQ_CS_ZT;"; // 查询状态
			var query = this._parameterRepository.GetDbContext().Set<Parameter>().FromSqlRaw(sql);
			return query;
		}

		/// <summary> 获取国籍参数表
		/// </summary>
		/// <returns></returns>
		public IQueryable<Parameter> QueryNationality()
		{
			var sql = "select a.gjdm as Id, a.mc as Value from yw_cs_gjdm a order by a.gjdm";
			return this._parameterRepository.GetDbContext().Set<Parameter>().FromSqlRaw(sql);
		}

		/// <summary> 房屋用途参数表
		/// </summary>
		/// <returns></returns>
		public IQueryable<Parameter> QueryUsageOfTheHouse()
		{
			var sql = "select a.FWYTDM as Id, a.MC as Value from yw_cs_fwyt a";
			return this._parameterRepository.GetDbContext().Set<Parameter>().FromSqlRaw(sql);
		}

		/// <summary> 
		/// 获取案件审批状态
		/// </summary>
		/// <returns></returns>
		public IQueryable<Parameter> QueryCaseStatus()
		{
			var sql = "select a.AJSPZTDM as Id, a.MC as Value from Clfwq_Cs_Ajspzt a";
			return this._parameterRepository.GetDbContext().Set<Parameter>().FromSqlRaw(sql);
		}

		/// <summary>
		/// 获取申请状态参数表
		/// </summary>
		/// <returns></returns>
		public IQueryable<Parameter> QueryApplyStatus()
		{
			var sql = "select a.SQZTDM as Id, a.MC as Value from YW_CS_SQZT a";
			return this._parameterRepository.GetDbContext().Set<Parameter>().FromSqlRaw(sql);
		}

		/// <summary>
		/// 获取资料审查状态参数表
		/// </summary>
		/// <returns></returns>
		public IQueryable<Parameter> QueryMaterialAudit()
		{
			var sql = "select a.SQZTDM as Id, a.MC as Value from YW_CS_SQZT a";
			return this._parameterRepository.GetDbContext().Set<Parameter>().FromSqlRaw(sql);
		}
		#endregion
		

	}
}
